package com.kura.uednews.fragment;

import java.util.ArrayList;

import com.kura.uedews.R;
import com.kura.uednews.db.newsDB;
import com.kura.uednews.model.Categories;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.transition.ChangeBounds;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class fragment_cate_list extends Fragment {

	ListView cateList;
	MyAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_cate_list, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		adapter = new MyAdapter(getActivity(), -1);
		cateList = (ListView) getActivity().findViewById(R.id.cateList);
		cateList.setAdapter(adapter);
		cateList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				adapter.changeData(
						((Categories) adapter.getItem(position)).getId(),
						((Categories) adapter.getItem(position)).getParent());
				cateList.setAdapter(adapter);
			}
		});
	}

}

class MyAdapter extends BaseAdapter {

	Context context;
	newsDB db;
	ArrayList<Categories> list;
	TextView cateTitle;
	CheckBox cateSubs;

	public MyAdapter() {

	}

	public MyAdapter(Context context, int type) {
		this.context = context;
		db = new newsDB(context);
		// Lay danh sach tat ca cate
		changeData(type, -1);
	}

	public void changeData(int id, int parent) {
		if (list != null) {
			list.clear();
		}
		if (id == -1) {
			list = db.getListAllRoot();
		} else {
			if ((db.getListAllByParent(id)).toArray().length > 0) {
				list = db.getListAllByParent(id);
			}
			if (parent == 0) {
				list.add(0, new Categories(-1, "..", "", 0, 0));
			} else {
				list.add(0, new Categories(parent, "..", "", 0, 0));
			}

		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.toArray().length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.custom_cate_row, parent, false);
		} else {
			row = convertView;
		}
		cateTitle = (TextView) row.findViewById(R.id.cateTitle);
		cateSubs = (CheckBox) row.findViewById(R.id.cateSubs);
		cateTitle.setText(list.get(position).getName());
		cateSubs.setOnCheckedChangeListener(null);
		if (list.get(position).getSubscribe() == 1) {
			System.out.println(list.get(position).getName() + " - true");
			cateSubs.setChecked(true);
		} else if (list.get(position).getSubscribe() == 0) {
			cateSubs.setChecked(false);
		}
		cateSubs.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				int id = list.get(position).getId();
				if (db.getSubscribeById(id) == 1) {
					db.setSubscribe(list.get(position).getId(), 0);
					Toast.makeText(context,
							"Đã hủy đăng kí - " + list.get(position).getName(),
							Toast.LENGTH_SHORT).show();
				} else if (db.getSubscribeById(id) == 0) {
					db.setSubscribe(list.get(position).getId(), 1);
					Toast.makeText(context,
							"Đã đăng kí - " + list.get(position).getName(),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		return row;
	}

}
